<?php
class Creditor
{
	public function construct()
	{
		
	}

        public function index()
        { 
               CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile("model/Creditor.php");
		$smarty = new Smarty();
		$modCreditor = new ModelCreditor();
                        $config=array();
			//$page =(int)$request['param1'];
                       // $limit = 'LIMIT 0, 500 ' ;
                        $perpage = ITEMPERPAGE;
                        $totalrowcount=$modCreditor->gettotalrowcount();
                        $curPage = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit = 'LIMIT  '.($curPage-1)*$perpage." , " . $perpage;
                        $Creditor_arr=$modCreditor->getCreditorlist($limit);
               // print_r($Creditor_arr);
                        $paginatorHelper = new Adminpagination();
                        $config['base_url'] = "order/index/";
                        $config['total_rows'] = $totalrowcount;
                        $config['per_page'] = $perpage;
                        $config['cur_page'] = $curPage;
                        $paginatorHelper->initialize($config);
                        $paginatorhtml=$paginatorHelper->create_links();
		$smarty->debugging = false;
		$smarty->caching = false;
		$smarty->cache_lifetime = 120;
		$smarty->assign("title", "Creditor Management");
		$smarty->assign("creditor_arr", $Creditor_arr);
                $smarty->assign("paginatorhtml", $paginatorhtml);
		$smarty->display('creditor/index.tpl');
        }

        public function addCreditor()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");
		CommonFunc::loadFile('model/Category.php');
		CommonFunc::loadFile('model/Product.php');
		$smarty = new Smarty();
		$smarty->assign("title", "Add Creditor");
		$smarty->display("Creditor/addCreditor.tpl");
	}

         

             public function viewcreditaccount()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");
		CommonFunc::loadFile('model/Creditor.php');
                $accounthtml="";
                $accountbalance=0;
                $transtype=array(0=>"Intialized credit account",1=>"Assigned Credit by admin",2=>"Removed credit by admin",3=>"Credit purchase-delivered",4=>"Payment recieved",5=>"correctin-removed payment",6=>"correction-removed payment",8=>"correction-removed delivery");
                $modCreditor=new ModelCreditor();
                $request = CommonFunc::getRequest();
		$userid = $request['param1'];
                $email=DB::fetchRow(" select user_email as email from tbl_user where  user_id=".$userid."");
                //echo $Creditorid;
		$creditor_arr = $modCreditor->getCreditorbyID($userid);

                foreach($creditor_arr as $arr){
                
                if(!empty($arr["orderid"])){
                        $orderid="<a onclick=loadPage('order/view/".$arr["orderid"]."')  href='javascript: void(0);'><strong> ( Order id ".$arr["orderid"]." )</strong></a>";
                    }else{
                        $orderid="";
                 }
                $accounthtml.="<tr>";
                $accounthtml.=" <td width='16%'>".$arr["transdate"]."</td>";
		$accounthtml.="<td width='24%'>".$transtype[$arr["transtype"]]." ".$orderid." </td>";
                if(in_array($arr["transtype"], array(0,1,4,8))){
                    $accountbalance+=(float)$arr["transvalue"];
                    $accounthtml.="<td width='12%'>".number_format($arr["transvalue"], 2)."</td>";
                    $accounthtml.="<td width='12%'>&nbsp;</td>";
                }
                else{
                    $accountbalance-=(float)$arr["transvalue"];
                    $accounthtml.="<td width='12%'>&nbsp;</td>";
                    $accounthtml.="<td width='12%'>".number_format($arr["transvalue"], 2)."</td>";
                    
                }
                $accounthtml.="<td width='16%'>".number_format($accountbalance, 2)."</td>";
                $accounthtml.="</tr>";
                }
               // echo $accounthtml;
		$smarty = new Smarty();
                $smarty->assign("creditor_arr", $creditor_arr);
                $smarty->assign("userid", $userid);
                $smarty->assign("accounthtml", $accounthtml);
                $smarty->assign("transtype", $transtype);
		$smarty->assign("title", "Credit account of ".$email["email"]);
		$smarty->display("creditor/creditaccount.tpl");
	}

	
	public function authCreditor($Creditorname, $password)
	{
		CommonFunc::loadFile('model/Creditor.php');
		$modCreditor = new ModelCreditor();
		if($Creditorname == "")
		{
			echo "<span class='error'>Creditorname cannot be empty</span>|error";
		} else if($password == "") {
			echo "<span class='error'>Password cannot be empty</span>|error";
		} else {		
			$Creditor_arr = $modCreditor->getCreditorInfo($Creditorname, $password);
			if(count($Creditor_arr) > 1)
			{
				$adminId = $Creditor_arr['admin_id'];
				$adminFname = $Creditor_arr['admin_firstname'];
				$adminLname = $Creditor_arr['admin_lastname'];
				session_register('admin_id');
				$_SESSION['admin_id'] = $adminId;
				$_SESSION['adminFname'] = $adminFname;
				$_SESSION['adminLname'] = $adminLname;
				echo "<span class='success'>Creditor Validation Completed</span>|success";
			} else {
				echo "<span class='error'>Invalid Creditorname or Password</span>|error";
			}
		}	
	}	
}
?>